﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace Common
{
    public static class StringEx
    {
        public static String Strip(this String str, params Char[] characters)
        {
            StringBuilder sb = new StringBuilder(str.Length);

            foreach (Char c in str)
            {
                if (Array.IndexOf<Char>(characters, c) < 0)
                {
                    sb.Append(c);
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// Counts the occurances of a character in a string.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        public static Int32 CountOccurances(this String str, Char c)
        {
            Int32 count = 0;

            foreach (Char s in str)
            {
                if (s == c)
                {
                    count++;
                }
            }

            return count;
        }

        //public static String Join(String seperator, params object[] objects)
        //{
        //    return Join(seperator, (System.Collections.IEnumerable)objects);
        //}

        public static String Join(String seperator, IEnumerable collection)
        {
            StringBuilder sb = new StringBuilder();
            Boolean first = true;

            foreach (Object obj in collection)
            {
                if (!first)
                {
                    sb.Append(seperator);
                }
                else
                {
                    first = false;
                }

                if (obj != null)
                {
                    if (obj.GetType().IsArray)
                    {
                        sb.Append(obj.GetType().Name);
                        sb.Append(" { ");
                        sb.Append(StringEx.Join(", ", (Array)obj));
                        sb.Append(" }");
                    }
                    else
                    {
                        sb.Append(obj.ToString());
                    }
                }
            }

            return sb.ToString();
        }

        public static String Join<T>(String separator, IEnumerable<T> objects, Converter<T, String> toString)
        {
            StringBuilder sb = new StringBuilder();

            Boolean first = true;

            foreach (T o in objects)
            {
                if (!first)
                {
                    sb.Append(separator);
                }
                else
                {
                    first = false;
                }

                sb.Append(toString(o));
            }

            return sb.ToString();
        }

        public static String Join(String separator, IEnumerable objects, String displayMember)
        {
            StringBuilder sb = new StringBuilder();

            Boolean first = true;

            foreach (Object o in objects)
            {
                if (!first)
                {
                    sb.Append(separator);
                }
                else
                {
                    first = false;
                }

                if (o != null)
                {
                    PropertyInfo member = o.GetType().GetProperty(displayMember);

                    if (member == null)
                    {
                        throw new ArgumentException("Display Member Not Found.", "displayMember");
                    }

                    Object memberValue = member.GetValue(o, new Object[] { });

                    if (memberValue != null)
                    {
                        sb.Append(memberValue);
                    }
                }
            }

            return sb.ToString();
        }
    }
}
